package com.yumingjiang;

import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;

/**
 * @ImportResource： 在springboot项目中除了使用注解管理Bean外，我们也可以用自定义的xml来配置bean以外，
 * 使用@ImportResource注解来导入到容器中
 */
@SpringBootApplication
@ImportResource(locations = "application-dev.xml")
public class DT2025Application implements CommandLineRunner, ApplicationContextAware {

    @Value("${spring.application.name}")
    private String appName;

    private static ApplicationContext applicationContext;

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        SpringApplication.run(DT2025Application.class);
        long endTime = System.currentTimeMillis();
        System.out.println("application started used "+(endTime-startTime)+"s.");

        System.out.println("------verify FactoryBean interface------");
        Object bean1 = applicationContext.getBean("aFactory");
        System.out.println("bean:"+bean1);//bean:com.yumingjiang.expand.beancreate.A@671d97bc
        Object bean = applicationContext.getBean("&aFactory");
        System.out.println("bean:"+bean);//bean:com.yumingjiang.expand.beancreate.AFactory@3cd46491

        System.out.println(JSONObject.toJSONString(applicationContext.getBean("b")));
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("======="+appName+"=======");
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

        DT2025Application.applicationContext =applicationContext;
    }
}